package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Loans;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class Register_EditChecks extends ActionSupport{
  private LoansDAO loanapp = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");

  private List<Loans>      gridModel;
  private List<Loans>      loans;
  private Integer             rows             = 0;
  private Integer             page             = 0;
  private Integer             total            = 0;
  private Integer             record           = 0;
  private String              sord;
  private String              sidx;
  private String              searchField;
  private String              searchString;
  private String              searchOper;
  private boolean             loadonce         = false;


  @Actions( {
    @Action(value = "/editregister", results = {
      @Result(name = "success", type = "json")
    })
  })
  public String execute()
  {
   try{   
       selected = (String) ServletActionContext.getRequest().getSession().getAttribute("selected");      
             
       int to = (getRows() * getPage());
            int from = to - getRows();
    
            String[] sel = selected.split(",");
            Integer[] sel_int = new Integer[sel.length];
            int count = 0;
            for (String s : sel) {
                sel_int[count] = Integer.parseInt(s);
                count++;

            }


            DetachedCriteria criteria = DetachedCriteria.forClass(Loans.class);
            criteria.add(Restrictions.in("loanid", sel_int));
//        for (String s : sel) {
//            try {
//                int id = Integer.parseInt(s);
//                loanapps.add(loanapp.get(id));
//            } catch (Exception e) {
//                return ERROR;
//            }
//        }

            record = loanapp.countByCriteria(criteria);
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);


            if (to > getRecord()) {
                to = getRecord();
            }

            if (loadonce) {
                setGridModel(loanapp.findByCriteria(criteria, from, to));
            } else {
                setGridModel(loanapp.findByCriteria(criteria, from, to));
            }

            setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           // LoanapplicationDAO.close();
        }
        return SUCCESS;
  }

  public String getJSON()
  {
    return execute();
  }

  /**
   * @return how many rows we want to have into the grid
   */
  public Integer getRows()
  {
    return rows;
  }

  /**
   * @param rows
   *          how many rows we want to have into the grid
   */
  public void setRows(Integer rows)
  {
    this.rows = rows;
  }

  /**
   * @return current page of the query
   */
  public Integer getPage()
  {
    return page;
  }

  /**
   * @param page
   *          current page of the query
   */
  public void setPage(Integer page)
  {
    this.page = page;
  }

  /**
   * @return total pages for the query
   */
  public Integer getTotal()
  {
    return total;
  }

  /**
   * @param total
   *          total pages for the query
   */
  public void setTotal(Integer total)
  {
    this.total = total;
  }

  /**
   * @return total number of records for the query. e.g. select count(*) from
   *         table
   */
  public Integer getRecord()
  {
    return record;
  }

  /**
   * @param record
   *          total number of records for the query. e.g. select count(*) from
   *          table
   */
  public void setRecord(Integer record)
  {

    this.record = record;

    if (this.record > 0 && this.rows > 0)
    {
      this.total = (int) Math.ceil((double) this.record / (double) this.rows);
    }
    else
    {
      this.total = 0;
    }
  }

  /**
   * @return an collection that contains the actual data
   */
  public List<Loans> getGridModel()
  {
    return gridModel;
  }

  /**
   * @param gridModel
   *          an collection that contains the actual data
   */
  public void setGridModel(List<Loans> gridModel)
  {
    this.gridModel = gridModel;
  }

  /**
   * @return sorting order
   */
  public String getSord()
  {
    return sord;
  }

  /**
   * @param sord
   *          sorting order
   */
  public void setSord(String sord)
  {
    this.sord = sord;
  }

  /**
   * @return get index row - i.e. user click to sort.
   */
  public String getSidx()
  {
    return sidx;
  }

  /**
   * @param sidx
   *          get index row - i.e. user click to sort.
   */
  public void setSidx(String sidx)
  {
    this.sidx = sidx;
  }

  public void setSearchField(String searchField)
  {
    this.searchField = searchField;
  }

  public void setSearchString(String searchString)
  {
    this.searchString = searchString;
  }

  public void setSearchOper(String searchOper)
  {
    this.searchOper = searchOper;
  }

  public void setLoadonce(boolean loadonce)
  {
    this.loadonce = loadonce;
  }
    
    private String selected;

    public void setSelected(String selected) {
        this.selected = selected;
    }

}
